<template>
  <view class="convenience-page">
    <!-- 搜索栏 -->
    <view class="search-section">
      <u-search 
        v-model="searchKeyword" 
        placeholder="搜索服务项目"
        :show-action="false"
        bg-color="#f5f5f5"
        @search="handleSearch"
        @clear="handleClear"
      ></u-search>
    </view>

    <!-- 服务分类 -->
    <view class="category-section">
      <scroll-view scroll-x enable-flex class="category-scroll">
        <view class="category-list">
          <view 
            v-for="(item, index) in categoryList" 
            :key="index"
            class="category-item"
            :class="{ active: currentCategory === item.value }"
            @click="handleCategoryChange(item.value)"
          >
            <view class="category-icon">
              <u-icon :name="item.icon" size="20" :color="currentCategory === item.value ? '#fa8c16' : '#999'"></u-icon>
            </view>
            <text class="category-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 服务列表 -->
    <view class="service-list">
      <view 
        v-for="(item, index) in serviceList" 
        :key="item.id"
        class="service-item"
        @click="handleItemClick(item)"
      >
        <!-- 服务状态标签 -->
        <view class="status-badge" :class="getStatusClass(item.status)">
          <text class="status-text">{{ item.statusText }}</text>
        </view>

        <!-- 服务内容 -->
        <view class="service-content">
          <view class="content-header">
            <text class="service-title">{{ item.title }}</text>
          </view>
          <view class="service-type-row">
            <view class="service-type">
              <u-tag 
                :text="item.category" 
                type="warning"
                size="mini"
              ></u-tag>
            </view>
          </view>
          
          <view class="content-body">
            <text class="service-description">{{ item.description }}</text>
          </view>
          
          <view class="content-footer">
            <view class="service-info">
              <view class="info-item">
                <u-icon name="user" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.provider }}</text>
              </view>
              <view class="info-item">
                <u-icon name="map-pin" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.location }}</text>
              </view>
              <view class="info-item">
                <u-icon name="clock" size="12" color="#999"></u-icon>
                <text class="info-text">{{ item.availableTime }}</text>
              </view>
            </view>
          </view>
          
          <view class="price-button-row">
            <view class="service-price">
              <text class="price-text" :class="{ free: item.price === 0 }">
                {{ item.price === 0 ? '免费' : `¥${item.price}` }}
              </text>
            </view>
            
            <view class="booking-btn" :class="getButtonClass(item.status)" @click.stop="handleBooking(item)">
              <text class="btn-text">{{ getButtonText(item.status) }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="serviceList.length === 0" class="empty-state">
      <u-icon name="tools" size="48" color="#ccc"></u-icon>
      <text class="empty-text">暂无服务信息</text>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more">
      <u-loading-icon v-if="loading" mode="circle"></u-loading-icon>
      <text v-else class="load-text" @click="loadMore">点击加载更多</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 当前分类
const currentCategory = ref('all')

// 分类列表
const categoryList = reactive([
  { label: '全部', value: 'all', icon: 'grid' },
  { label: '家政服务', value: 'housekeeping', icon: 'home' },
  { label: '维修服务', value: 'repair', icon: 'setting' },
  { label: '清洁服务', value: 'cleaning', icon: 'star' },
  { label: '配送服务', value: 'delivery', icon: 'car' },
  { label: '其他服务', value: 'other', icon: 'more-circle' }
])

// 服务列表数据
const serviceList = ref([
  {
    id: 1,
    title: '家庭保洁服务',
    description: '专业保洁团队，提供家庭深度清洁服务，包括客厅、卧室、厨房、卫生间等全方位清洁...',
    category: '家政服务',
    status: 1,
    statusText: '可预约',
    provider: '绿洁家政',
    location: '社区内',
    availableTime: '8:00-18:00',
    price: 80
  },
  {
    id: 2,
    title: '家电维修服务',
    description: '专业家电维修师傅，提供空调、洗衣机、冰箱、电视等家电维修服务，上门服务...',
    category: '维修服务',
    status: 1,
    statusText: '可预约',
    provider: '快修师傅',
    location: '社区内',
    availableTime: '9:00-17:00',
    price: 50
  },
  {
    id: 3,
    title: '老人陪护服务',
    description: '专业护理人员，提供老人日常陪护、生活照料、健康监测等服务，贴心周到...',
    category: '家政服务',
    status: 1,
    statusText: '可预约',
    provider: '康乐护理',
    location: '社区内',
    availableTime: '24小时',
    price: 120
  },
  {
    id: 4,
    title: '快递代收服务',
    description: '社区快递代收点，提供快递代收、代发、暂存等服务，方便居民生活...',
    category: '配送服务',
    status: 1,
    statusText: '可预约',
    provider: '社区服务中心',
    location: '社区服务中心',
    availableTime: '8:00-20:00',
    price: 0
  },
  {
    id: 5,
    title: '法律咨询服务',
    description: '专业律师团队，提供法律咨询、合同审查、纠纷调解等服务，维护居民权益...',
    category: '咨询服务',
    status: 2,
    statusText: '已满员',
    provider: '正义律师事务所',
    location: '社区会议室',
    availableTime: '周六 14:00-16:00',
    price: 0
  },
  {
    id: 6,
    title: '宠物寄养服务',
    description: '专业宠物寄养服务，提供宠物日常照料、健康监测、遛狗等服务，让您放心出行...',
    category: '其他服务',
    status: 1,
    statusText: '可预约',
    provider: '爱宠之家',
    location: '社区内',
    availableTime: '24小时',
    price: 60
  }
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取状态样式类
const getStatusClass = (status: number) => {
  switch (status) {
    case 1: return 'status-available'
    case 2: return 'status-full'
    case 3: return 'status-unavailable'
    default: return 'status-default'
  }
}

// 获取按钮样式类
const getButtonClass = (status: number) => {
  switch (status) {
    case 1: return 'btn-booking'
    case 2: return 'btn-full'
    case 3: return 'btn-unavailable'
    default: return 'btn-default'
  }
}

// 获取按钮文本
const getButtonText = (status: number) => {
  switch (status) {
    case 1: return '立即预约'
    case 2: return '已满员'
    case 3: return '暂不可用'
    default: return '查看详情'
  }
}

// 搜索处理
const handleSearch = (value: string) => {
  console.log('搜索服务:', value)
  // 这里实现搜索逻辑
}

// 清空搜索
const handleClear = () => {
  searchKeyword.value = ''
  console.log('清空搜索')
}

// 分类改变
const handleCategoryChange = (value: string) => {
  currentCategory.value = value
  console.log('分类改变:', value)
  // 这里实现分类筛选逻辑
}

// 点击服务项
const handleItemClick = (item: any) => {
  console.log('点击服务:', item)
  // 跳转到服务详情页面
  uni.navigateTo({
    url: `/pages/convenience-service/detail?id=${item.id}`
  })
}

// 预约处理
const handleBooking = (item: any) => {
  if (item.status === 1) {
    // 跳转到预约页面
    uni.navigateTo({
      url: `/pages/convenience-service/booking?id=${item.id}`
    })
  } else if (item.status === 2) {
    uni.showToast({
      title: '服务已满员',
      icon: 'none'
    })
  } else if (item.status === 3) {
    uni.showToast({
      title: '服务暂不可用',
      icon: 'none'
    })
  }
}

// 加载更多
const loadMore = () => {
  if (loading.value) return
  
  loading.value = true
  setTimeout(() => {
    // 模拟加载更多数据
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  console.log('便民服务页面加载完成')
})
</script>

<style lang="scss" scoped>
.convenience-page {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 0 0 20rpx 0;
}

.search-section {
  padding: 24rpx 32rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.category-section {
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  
  .category-scroll {
    white-space: nowrap;
    padding: 0 24rpx 24rpx 24rpx;
    
    .category-list {
      display: flex;
      padding: 24rpx 0 0 0;
      width: max-content;
      
      .category-item {
        flex-shrink: 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 6rpx;
        padding: 16rpx 12rpx;
        margin-right: 12rpx;
        border-radius: 12rpx;
        background-color: #f8f9fa;
        transition: all 0.3s ease;
        min-width: 100rpx;
        border: 2rpx solid transparent;
        
        &.active {
          background: linear-gradient(135deg, #fff7e6 0%, #fff2d9 100%);
          border: 2rpx solid #fa8c16;
          transform: translateY(-2rpx);
          box-shadow: 0 4rpx 12rpx rgba(250, 140, 22, 0.15);
        }
        
        .category-icon {
          width: 40rpx;
          height: 40rpx;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        
        .category-text {
          font-size: 22rpx;
          color: #666;
          text-align: center;
          font-weight: 500;
          white-space: nowrap;
        }
      }
    }
  }
}

.service-list {
  padding: 0 32rpx;
  
  .service-item {
    background-color: #fff;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    padding: 32rpx;
    position: relative;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    border: 1rpx solid rgba(0, 0, 0, 0.04);
    
    &:active {
      transform: translateY(-2rpx);
      box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
    }
    
    .status-badge {
      position: absolute;
      top: 20rpx;
      right: 20rpx;
      padding: 8rpx 16rpx;
      border-radius: 20rpx;
      font-size: 20rpx;
      
      &.status-available {
        background-color: #52c41a;
        color: #fff;
      }
      
      &.status-full {
        background-color: #ff4d4f;
        color: #fff;
      }
      
      &.status-unavailable {
        background-color: #d9d9d9;
        color: #666;
      }
    }
    
    .service-content {
      .content-header {
        margin-bottom: 16rpx;
        
        .service-title {
          font-size: 32rpx;
          font-weight: 600;
          color: #333;
          line-height: 1.4;
          word-break: break-all;
        }
      }
      
      .service-type-row {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 20rpx;
        
        .service-type {
          flex-shrink: 0;
        }
      }
      
      .content-body {
        margin-bottom: 20rpx;
        
        .service-description {
          font-size: 28rpx;
          color: #666;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
      
      .content-footer {
        margin-bottom: 20rpx;
        
        .service-info {
          display: flex;
          flex-direction: column;
          gap: 12rpx;
          
          .info-item {
            display: flex;
            align-items: center;
            gap: 8rpx;
            
            .info-text {
              font-size: 24rpx;
              color: #999;
            }
          }
        }
      }
      
      .price-button-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 20rpx;
        
        .service-price {
          flex: 1;
          
          .price-text {
            font-size: 28rpx;
            font-weight: 600;
            color: #ff4d4f;
            
            &.free {
              color: #52c41a;
            }
          }
        }
        
        .booking-btn {
          flex-shrink: 0;
          padding: 16rpx 32rpx;
          border-radius: 40rpx;
          font-size: 24rpx;
          font-weight: 500;
          transition: all 0.3s ease;
          
          &.btn-booking {
            background-color: #fa8c16;
            color: #fff;
            
            &:active {
              background-color: #d46b08;
            }
          }
          
          &.btn-full {
            background-color: #ff4d4f;
            color: #fff;
          }
          
          &.btn-unavailable {
            background-color: #d9d9d9;
            color: #666;
          }
          
          .btn-text {
            font-size: 24rpx;
          }
        }
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
  
  .load-text {
    font-size: 28rpx;
    color: #fa8c16;
  }
}
</style>
